#include "Timer16Bit.h"
#include<avr/io.h>
#include<avr/interrupt.h>
#include "../../../../bit/bit.h"
#include "../../../../counter/Counter.h"
#include "../../../../flag/Flag.h"

using namespace Hardware::Avr;

//use this counter for the timer.
Counter<unsigned int> count = Counter<unsigned int>(0, 10);
volatile unsigned char flag = 0;

Flag f = Flag();

int main()
{
	DDRA = 0xFF;
	PORTA = 0x00;
	DDRB = 0xFF;
	PORTB = 0xFF;

	Timer16BitConfig config = Timer16BitConfig();

	config.outputCompareA = Timer16Bit::ClearOnCompare;
	config.forceA = 0;
	config.waveformMode = Timer16Bit::CTC_Mode;
	config.clockSelect = Timer16Bit::Prescale64;
	config.outputCountA = 1000;
	config.count = 0;
	config.compareAInterrupt = 1;

	Timer16Bit timer = Timer16Bit(config);
	//configure timer.
	timer.config(); //configure timer.


	//TCCR1B = 0x0A;
	//OCR1A = 1000;
	//TIMSK = 0x12;
	//TCNT1 = 0;


	sei();
	//do a loop test here for fun
	while(true)
	{
		if(f[0].getBit()==1)
		{
			//reset the flag
			f[0].setBit(0);
			PORTA = ~PORTA; //flash the port light.
		}
	}
}


ISR(TIMER1_COMPA_vect)
{
	count++;
	if(count.flag()==true)
	{
		count.resetCount(0);
		//count = 0;
		//flag = 1;
		f[0].setBit(1);
	}
}

